package com.my.admin.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.tree.TreeUtil;
import com.my.admin.entity.Menu;
import com.my.admin.service.MenuService;
import com.my.admin.service.ShiroService;
import com.my.admin.util.Rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
public class IndexController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private ShiroService shiroService;

    @GetMapping("menu")
    public Rest logout(@RequestHeader("authentication-token") String token) {
        List<Menu> list = menuService.getMenuByUserId(Convert.toLong(shiroService.getTokenInfo(token).get("userId")));
        return Rest.ok("菜单请求成功！").setData("menus", TreeUtil.build(list, 0L, (treeNode, tree) -> {
            tree.setId(treeNode.getId());
            tree.setName(treeNode.getName());
            tree.setParentId(treeNode.getParentId());
            tree.putExtra("url", treeNode.getUrl());
            tree.putExtra("icon", treeNode.getIcon());
        }));
    }

}
